package com.hs.dnms.dwr;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;

import com.hs.core.dwr.DwrHelper;

public class DataManagerDwr   extends DwrHelper{
	/**
	 * 删除题录信息
	 * @param id
	 */
	public String deleteMagazine(String id){
		String isDelete = this.isDelete(id);
		String message = "";
		if("0".equals(isDelete)){
			message = "0";
		}else {
			this.getDbo().update("delete from dnms_magazine where id=?", new Object[]{id});
			message = "1";
		}
		return message;
		
	}
	/**
	 * 检查该题录信息是否可删除
	 * @param id
	 * @return
	 */
	public String isDelete(String id){
		List accList = this.getDbo().queryList("select to_char(count(*)) as countnum from dnms_magazine where state='0' and id=?", 
				new Object[]{id});
		Map data = (Map)accList.get(0);
		return (String)data.get("countnum");
	}
	
	/**
	 * 分配案件，方式一
	 * @param username  用户名
	 * @param casestr 		分配案件的主键，格式：1,2,3
	 */
	public String distributeFirst(String username,String casestr){
		casestr = casestr.substring(0,casestr.length()-1);
		String[] caseList = casestr.split(",");
		String message = "0";
		int num = 0;
		for(int i=0; i<caseList.length; i++){
			String oneCase = caseList[i];
			int result = this.getDbo().update("update dnms_magazine set dataproducer=?,state='1' where state='0' and id=?",
					new Object[]{username,oneCase});
			num = num + result ;
		}
		message = num + "";
		return message;
		
	}

	/**
	 * 分配案件，方式二
	 * @param username  用户名
	 * @param num 		分配案件个数
	 */
	public String distributeSecond(String username,String num){
		List accList = this.getDbo().queryList("select to_char(count(*)) as countnum from dnms_magazine where state='0'",null);
		Map data = (Map)accList.get(0);
		String casenum = (String)data.get("countnum");
		String message = "";
		if(Integer.parseInt(num) <= Integer.parseInt(casenum)){
			int rownum = Integer.parseInt(num) + 1;
			int result = this.getDbo().update("update dnms_magazine set dataproducer=?,state='1' where state='0' and rownum<=?",
					new Object[]{username,num});
			message = "0";
		}else {
			message = casenum;
		}
		return message;
		
	}
}
